//
//  GlobalData.m
//  Youhu
//
//  Created by Eton on 2016/10/25.
//  Copyright © 2016年 ApeStar. All rights reserved.
//

#import "GlobalData.h"
//#import "AutoCoding.h"
//#import "AppRequest+Account.h"
#import "AppRequest+Common.h"
#import "CommonModel.h"
//#import "GeTuiSdk.h"
#import "LoginModel.h"

@interface GlobalData()
@property (nonatomic, strong) RACCommand * commonCommand;
@property (nonatomic, assign) BOOL enbleGetCommon;

@end

@implementation GlobalData


#pragma mark - 单例

+ (GlobalData *)shared {
    
    static GlobalData *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[GlobalData alloc] init];
    });
    
    return instance;
}


#pragma mark - setup
- (void)setupAppBaseConfig{
    
//    [kNotification addObserver:self selector:@selector(requestErrorWithNotification:) name:kErrorNotificationName object:nil];
//    
//    
//    if([AuthorManager isLogin]) {
//        LoginResModel *login = [AuthorManager loginInfo];
//        [[ApeSessionManager sharedManager] setTicket:login.ticket];
//        [[GlobalData shared] updateUserAccount];
//    }else{
//        [[ApeSessionManager sharedManager] setTicket:nil];
//    }
    [self updateBaseData];
}

#pragma mark - Get

- (CommonModel *)commonData{
    if (!_commonData) {
        [self updateBaseData];
    }
    return _commonData;
}

- (RACCommand *)commonCommand{
    if (!_commonCommand) {
        _commonCommand = [[RACCommand alloc] initWithEnabled:RACObserve(self, enbleGetCommon) signalBlock:^RACSignal *(id input) {
            return [AppRequest getCommonList];
        }];
        [_commonCommand.executing subscribeNext:^(id x) {
            self.enbleGetCommon = ![x boolValue];
        }];
    }
    
    return _commonCommand;
}

#pragma mark - Request

- (void)updateBaseData{
    [self.commonCommand execute:nil];
}

- (void)updateUserAccount {
    
}

#pragma mark - 登录逻辑

- (BOOL)isLogin{
//    if ([AuthorManager isLogin]) {
        return YES;
//    }else{
//        [kGlobalVC gotoLogin:NO];
//        return NO;
//    }
}

- (void)loginSuccessWithRes:(LoginModel *)loginRes{
    
//    [AuthorManager setLoginInfo:loginRes];
//    [AuthorManager saveLoginDate];
//    
//    [[ApeSessionManager sharedManager] setTicket:loginRes.ticket];
//    [kNotification postNotificationName:K_LOGIN_LOGOUT_NOTIFICATION object:nil];
//    
//    [[GlobalData shared] updateUserAccount];
    
//    [kUserDefaults setObject:loginRes.user_phone forKey:UD_LOGIN_USERNAME_PREFERENCE_KEY];
//    [kUserDefaults synchronize];
    
//    if (loginRes.user_id.length > 0) {
//        [GeTuiSdk bindAlias:loginRes.user_id andSequenceNum:loginRes.user_id];
//    }
}

- (void)logoutWithFail:(ApeResponseError *)failureResponse{
//    [GeTuiSdk unbindAlias:[AuthorManager loginInfo].user_id andSequenceNum:[AuthorManager loginInfo].user_id];
//    [AuthorManager clearLoginUserInfo];
//    [[ApeSessionManager sharedManager] setTicket:nil];
//    [kNotification postNotificationName:K_LOGIN_LOGOUT_NOTIFICATION object:nil];
//    
//    if (failureResponse.errorMessage.tx_isNotNull) {
//        [MBProgressHUD tx_showError:failureResponse.errorMessage];
//    }
//    if (failureResponse) {
//        [AuthorManager postLoginStatusInvalidNotification:failureResponse];
//        //被顶号时，调到登录页面后-返回到首页
//        [kGlobalVC gotoLogin:NO];
//        [[GlobalVC shared] backHome];
//    }else{
//        [kGlobalVC gotoLogin:NO];
//        //退出登录时，当前界面返回到首页
//        [[GlobalVC shared] backHome];
//    }
}


#pragma mark - 服务器时间

//- (NSDate *)serviceDate{
//    NSTimeInterval timeDifference = [kUserDefaults doubleForKey:UD_SERVICE_TIME_DIFFERENCE];
//    NSDate * sDate = [NSDate dateWithTimeIntervalSinceNow:timeDifference];
//    NSLog(@"serviceDate:%@",sDate);
//    return sDate;
//}
//
//- (void)setServiceDate:(NSDate *)serviceDate{
//    NSDate * sysdate = [NSDate date];
//    NSTimeInterval timeDifference = -[sysdate timeIntervalSinceDate:serviceDate];
//    [kUserDefaults setDouble:timeDifference forKey:UD_SERVICE_TIME_DIFFERENCE];
//    [kUserDefaults synchronize];
//    NSLog(@"_timeDifference:%f",timeDifference);
//}


#pragma mark - 消息为读数

//- (void)setAnnounUnreadMessagesCount:(int)announUnreadMessagesCount{
//    [kUserDefaults setInteger:announUnreadMessagesCount forKey:UD_ANNOUN_UNREAD_COUNT_UPDATE];
//    [kUserDefaults synchronize];
//}
//
//- (int)announUnreadMessagesCount{
//   return (int)[kUserDefaults integerForKey:UD_ANNOUN_UNREAD_COUNT_UPDATE];
//}
//
//#pragma mark - kErrorNotificationName
//
//- (void)requestErrorWithNotification:(NSNotification *)notification{
//    NSDictionary * errorDict = notification.userInfo;
//    NSInteger errorCode = [[errorDict objectForKey:kErrorCodeResponseKey] integerValue];
//    NSInteger serviceErrorCode = [[errorDict objectForKey:kErrorCodeResponseKey] integerValue];
//    NSString *serviceErrorMessage = [errorDict objectForKey:kErrorMessageKey];
//    FailureResponseModel * failureResponse = [[FailureResponseModel alloc] initWithErrorCode:serviceErrorCode errorMessage:serviceErrorMessage];
//    
//    if (errorCode == ErrorCodeTypeLoginInvalid){
//        [kGlobalData logoutWithFail:failureResponse];
//    }
//    else if (errorCode == ErrorCodeTypeLoginInOtherDevice) {
//        // 账号从其他设备登录的弹窗
//        [UIAlertView alertViewWithTitle:nil message:serviceErrorMessage cancelButtonTitle:@"确定" otherButtonTitles:nil onDismiss:^(int buttonIndex) {
//            
//        } onCancel:^{
//            [kGlobalData logoutWithFail:failureResponse];
//        }];
//    }
//    else if (errorCode == ErrorCodeTypeTokenInvalid) {
//        NSNumber *  sDateNum = errorDict[@"data"];
//        self.serviceDate = [NSDate dateWithTimeIntervalSince1970:[sDateNum doubleValue] / 1000.0];
//    }else if (errorCode == ErrorCodeTypeServiceError404) {
//        
//    }
//}

#pragma mark - Privace Method

@end
